﻿

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iomanip>

using namespace std;

struct Student {
    string name;
    int id;
    vector<double> scores;
    double total;
    double average;
};

// 函数声明
void inputStudents(vector<Student>& students);
void calculateStudentGrades(vector<Student>& students);
void calculateClassStats(const vector<Student>& students, double& classAvg, double& classMax);
void sortStudents(vector<Student>& students);
void displayStudents(const vector<Student>& students);
void searchStudent(const vector<Student>& students);

int main() {
    vector<Student> students;
    int choice;
    double classAvg, classMax;

    do {
        cout << "\n========== 学生成绩管理系统 ==========\n";
        cout << "1. 输入学生信息和成绩\n";
        cout << "2. 计算每个学生的平均分和总分\n";
        cout << "3. 计算班级平均分和最高分\n";
        cout << "4. 按成绩排序并显示排名\n";
        cout << "5. 查找学生成绩信息\n";
        cout << "0. 退出\n";
        cout << "请选择操作：";
        cin >> choice;

        switch (choice) {
        case 1:
            inputStudents(students);
            break;
        case 2:
            calculateStudentGrades(students);
            displayStudents(students);
            break;
        case 3:
            calculateClassStats(students, classAvg, classMax);
            cout << "班级平均分：" << classAvg << "\n班级最高分：" << classMax << endl;
            break;
        case 4:
            sortStudents(students);
            displayStudents(students);
            break;
        case 5:
            searchStudent(students);
            break;
        case 0:
            cout << "感谢使用！\n";
            break;
        default:
            cout << "无效输入，请重新选择！\n";
        }
    } while (choice != 0);

    return 0;
}


void inputStudents(vector<Student>& students) {
    int n;
    cout << "请输入学生人数：";
    cin >> n;
    students.resize(n);
    for (int i = 0; i < n; i++) {
        cout << "输入第 " << i + 1 << " 个学生的姓名和学号：";
        cin >> students[i].name >> students[i].id;
        int m;
        cout << "请输入科目数量：";
        cin >> m;
        students[i].scores.resize(m);
        cout << "请输入 " << m << " 门成绩：";
        for (int j = 0; j < m; j++) {
            cin >> students[i].scores[j];
        }
    }
}


void calculateStudentGrades(vector<Student>& students) {
    for (auto& s : students) {
        s.total = 0;
        for (double score : s.scores) {
            s.total += score;
        }
        s.average = s.total / s.scores.size();
    }
}


void calculateClassStats(const vector<Student>& students, double& classAvg, double& classMax) {
    if (students.empty()) {
        classAvg = classMax = 0;
        return;
    }
    double sum = 0;
    classMax = students[0].total;
    for (const auto& s : students) {
        sum += s.total;
        if (s.total > classMax) classMax = s.total;
    }
    classAvg = sum / students.size();
}


void sortStudents(vector<Student>& students) {
    sort(students.begin(), students.end(), [](const Student& a, const Student& b) {
        return a.total > b.total;
        });
}


void displayStudents(const vector<Student>& students) {
    if (students.empty()) {
        cout << "暂无学生数据！\n";
        return;
    }
    cout << left << setw(10) << "姓名" << setw(10) << "学号" << setw(10) << "总分" << setw(10) << "平均分" << endl;
    for (const auto& s : students) {
        cout << left << setw(10) << s.name << setw(10) << s.id << setw(10) << s.total << setw(10) << s.average << endl;
    }
}


void searchStudent(const vector<Student>& students) {
    int id;
    cout << "请输入要查找的学号：";
    cin >> id;
    for (const auto& s : students) {
        if (s.id == id) {
            cout << "姓名：" << s.name << "\n学号：" << s.id << "\n总分：" << s.total << "\n平均分：" << s.average << endl;
            return;
        }
    }
    cout << "未找到该学生！\n";
}



